<h3>Listing messages</h3>

<table>
  <tr>
    <th>Message Type</th>
    <th>Message Time</th>
    <th>Sender</th>
    <th>Receiver</th>
    <th>Message History</th>
    <th>Handled</th>
    <th>Actions</th>
  </tr>

  <% @messages.each do |message| %>
    <tr>
      <td><%=h message.mtype %></td>
      <td><%=h message.mtime %></td>

      <td>
        <% if message.sender && User.find(message.sender) %>
          <%=h User.find(message.sender).name %>
        <% else %>
          <%=h "None" %>
        <% end %>
      </td>

      <td>
        <% if message.receiver && User.find(message.receiver) %>
          <%=h User.find(message.receiver).name %>
        <% else %>
          <%= "None" %>
        <% end %>
      </td>

      <td>
        <%= link_to 'Click Here', :controller => 'messages', :action => 'get_by_brid', :id => message.borrow_rec_id %>
      </td>

      <td><%=h message.handled %></td>

      <td>
        <% if message.receiver == session[:user_id] %>
          <% if message.handled == false && message.mtype == "REQUEST" %>
            <%= link_to 'Deliver', :controller => 'messages', :action => 'deliver', :id => message.id %>
            <%= link_to 'Decline', :controller => 'messages', :action => 'decline_request', :id => message.id %>
          <% end %>

          <% if message.handled == false && message.mtype == "DELIVER" %>
            <%= link_to 'Confirm', :controller => 'messages', :action => 'confirm', :id => message.id %>
          <% end %>

          <% if message.handled == false && message.mtype == "RESERVE" %>
            <%= link_to 'Accept', :controller => 'messages', :action => 'accept_reserve', :id => message.id %>
            <%= link_to 'Decline', :controller => 'messages', :action => 'decline_reserve', :id => message.id %>
          <% end %>

          <% if message.handled == false && message.mtype == "RENEW" %>
            <%= link_to 'Accept', :controller => 'messages', :action => 'accept_renew', :id => message.id %>
            <%= link_to 'Decline', :controller => 'messages', :action => 'decline_renew', :id => message.id %>
          <% end %>
        <% end %>
      </td>
    </tr>
  <% end %>
</table>
